var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    index: 0,
    index_url:"/pages/shop/shop"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shop_id: options.shop_id
    })
    var that = this;
    if (!app.globalData.userInfo) {
      app.getUserInfo();
    }
    this.setData({
      userInfo: app.globalData.userInfo,
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/shopInfo/openid/" + app.globalData.openid + '/shop_id/' + options.shop_id, function (res) {
      that.setData({
        info: res.data
      })
    })
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getJoinInfo", function (res) {
      that.setData({
        join: res.data
      })
    })
  },

  bindChange: function (e) {
    const val = e.detail.value;
    this.setData({
      index: val
    })
  },
  clickRenew: function (e) {
    var post_data = { shop_id: this.data.info['shop_id'], join_id: this.data.join[this.data.index]['id'], openid: app.globalData.openid,name:this.data.info['name']};
    wx.request({
      url: "https://www.58zltc.com/api.php/Shop/renew?token=" + this.data.token,
      method: 'POST',
      data: post_data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.err_code === 400) {
          wx.showModal({
            title: '提示',
            content: res.data.err_msg,
            showCancel: false
          })
        } else {
          var data = res.data;
          var prepay_id = res.data.package;
          var a = prepay_id.slice(10)
          //支付添加formid
          app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: a, openid: app.globalData.openid, type: 'pay' }, function (res) { })
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': res => {
              if (res.errMsg == 'requestPayment:ok') {
               
                wx.showModal({
                  title: '提示',
                  content: "支付成功！",
                  showCancel: false,
                  success: function (res) {
                    wx.redirectTo({
                      url: "/pages/mine/shop/shop"
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})